package com.lizemin.inputoutput.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author lzm
 * @date 2025/4/4 21:39
 * @description 配置使用Tomcat中的CsrfPreventionFilter
 */
@Slf4j
@Configuration
public class FilterConfig {

    /**
     * 配置csrf过滤器
     */
//    @Bean
//    public FilterRegistrationBean<CsrfPreventionFilter> csrfPreventionFilterRegistration() {
//        FilterRegistrationBean<CsrfPreventionFilter> registration = new FilterRegistrationBean<>();
//        registration.setFilter(new CsrfPreventionFilter());
//        registration.addUrlPatterns("/*"); // 过滤器映射到所有URL
//        // 设置入口点
//        registration.addInitParameter("entryPoints", "/login,/home");
//        // 设置缓存大小
//        registration.addInitParameter("nonceCacheSize", "5");
//        // 设置请求参数名
//        registration.addInitParameter("nonceRequestParameterName", "csrf_nonce");
//        registration.setName("csrfPreventionFilter");
//        registration.setOrder(1); // 设置过滤器执行顺序
//        return registration;
//    }

    /**
     * @see com.lizemin.inputoutput.config.FilterConfig#characterFilterRegistration()
     *
     * 必须要将字符编码过滤器注册为一个bean，才能禁用springMVC中关于此过滤器的自动配置
     * 我一开始在characterFilterRegistration方法中使用的是new CharacterEncodingFilter()，
     * 这样导致了注册失败，因为springboot检测到已经有这个配置了，所以就没有注册我的配置
     */
    @Bean
    public CharacterEncodingFilter characterEncodingFilter() {
        return new CharacterEncodingFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                log.info("进入了字符编码过滤器");
                super.doFilterInternal(request, response, filterChain);
                String reqEnc = request.getCharacterEncoding();
                String respEnc = response.getCharacterEncoding();
                log.info("修改后的字符编码是：请求编码为：{},响应编码为：{}", reqEnc, respEnc);
            }
        };
    }

    /**
     * 配置字符编码过滤器
     */
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> characterFilterRegistration() {
        log.info("characterFilterRegistration方法被调用");
        FilterRegistrationBean<CharacterEncodingFilter> registration = new FilterRegistrationBean<>();
        CharacterEncodingFilter filter = characterEncodingFilter();
        filter.setEncoding(StandardCharsets.UTF_8.name());
        filter.setForceEncoding(true);
        registration.setFilter(filter);
        registration.addUrlPatterns("/*"); // 过滤器映射到所有URL
        registration.setOrder(1); // 设置过滤器执行顺序
        return registration;
    }


}
